Java多线程中如何避免死锁

在Java中如何避免死锁,多线程开发时,经常会遇到这样的问题,什么是死锁,简单的说就是两个或多个线程,等待对方释放某个资源而卡死的情况称之为死锁

1.在Java代码中如何检查是否有死锁?

 ①在不借助其它工具的话,只能从代码中synchronized block和调用synchronized 方法的地方着手检查代码
 ②如果程序已经出现了死锁,可以通过获取Thread的dump来分析,在linux上可以执行"kill -3 pid",的方式发送一个SIGQUIT信号给Java应用之后,通常会有当前的Thread Dump输出,从Thread 的dump log可以看出那个Theread lock了那个object。
③另外一种方式是通过jconsole,可以清楚的看出那些Thread locked的

2.死锁的实例

public class DeadLockDemo {
    /*
     * * This method request two locks, first String and then Integer
     */
    public void method1() {
        synchronized (String.class) {
            System.out.println("Aquired lock on String.class object");
            synchronized (Integer.class) {
                System.out.println("Aquired lock on Integer.class object");
            }
        }
    } /*
       * * This method also requests same two lock but in exactly Opposite order
       * i.e. first Integer and then String. This creates potential deadlock, if
       * one thread holds String lock and other holds Integer lock and they wait
       * for each other, forever.
       */

    public void method2() {
        synchronized (Integer.class) {
            System.out.println("Aquired lock on Integer.class object");
            synchronized (String.class) {
                System.out.println("Aquired lock on String.class object");
            }
        }
    }
}
在上面的代码中,如果method1和method2同时被多个线程调用,就很容易出现死锁。线程A在执行 method1()时lock了String对象,线程B在执行method2()时lock了Integer对象,相互都在等待对方释放锁,从而出现了死锁,具体可以看下图

3.怎么避免死锁在java中?
面试时经常会被遇到如何避免死锁,如果你仔细看上面的代码,你可能发现死锁的真正原因不是多线程但他们请求锁的方式。如果你提供一个有序的访问,那么问题将得到解决
public class DeadLockFixed {
    /**
     * * Both method are now requesting lock in same order, first Integer and
     * then String. * You could have also done reverse e.g. first String and
     * then Integer, * both will solve the problem, as long as both method are
     * requesting lock * in consistent order.
     */
    public void method1() {
        synchronized (Integer.class) {
            System.out.println("Aquired lock on Integer.class object");
            synchronized (String.class) {
                System.out.println("Aquired lock on String.class object");
            }
        }
    }

    public void method2() {
        synchronized (Integer.class) {
            System.out.println("Aquired lock on Integer.class object");
            synchronized (String.class) {
                System.out.println("Aquired lock on String.class object");
            }
        }
    }
}

现在应该不会出现死锁了,因为method1和method2都是先lock Integer,然后lock String,当ThreadA调用method1 lock了Integer后ThreadB调用method2后,只有当ThreadA释放了Integer Object后method2中方法才会执行,从而有效的避免了死锁。




  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值